#include "list.h"
LTNode* BuyListNode(int x)
{
	LTNode* node = (LTNode*)malloc(sizeof(LTNode));
		if (node == NULL)
		{
			perror("malloc fail");
		}
		node->next = NULL;
		node->prev = NULL;
		node->date = x;
		return node;
}
LTNode* LTinit()
{
	LTNode* phead=BuyListNode(-1);
	phead->next = phead;
	phead->prev = phead;
	return phead;
}
void pushback(LTNode* phead,LTdatetype x)
{
	assert(phead);
	LTNode* newnode=BuyListNode(x);
	LTNode* tail = phead->prev;
	tail->next = newnode;
	newnode->prev = tail;
	newnode->next = phead;
	phead->prev = newnode;
}
void printListNode(LTNode* phead)
{
	assert(phead);
	printf("<=>head<=>");
	LTNode* cur=phead->next;
	while (cur != phead)
	{
		printf("%d<=>", cur->date);
		cur=cur->next;
	}
}
bool LTempty(LTNode* phead)
{
	assert(phead);
	//LTNode* cur = phead->next;
	if (phead->next == phead)
		return true;
	else
		return false;
}
void popback(LTNode* phead)
{
	assert(phead);
	assert(!LTempty(phead));
	LTNode* tail = phead->prev;
	LTNode* tailprev = tail->prev;
	tailprev->next = phead;
	phead->prev = tailprev;
	free(tail);
	tail = NULL;
}

void pushfront(LTNode* phead,int x)
{
	assert(phead);
	LTNode* newnode=BuyListNode(x);
	LTNode* first=phead->next;
	phead->next = newnode;
	newnode->prev = phead;
	newnode->next = first;
	first->prev = phead;
}
void popfront(LTNode* phead)
{
	assert(phead);
	LTNode* cur = phead->next;
	phead->next = cur->next;
	cur->next->prev = phead;
	free(cur);
	cur = NULL;
}
void insert(LTNode* pos, LTdatetype x)
{
	assert(pos);
	LTNode* newnode = BuyListNode(x);
	LTNode* p = pos->prev;
	p->next = newnode;
	newnode->next = pos;
	newnode->prev = p;
	pos->prev = newnode;
}
